﻿using System;
using System.Text;
using System.Linq;
#if NETFX_CORE
using XcoAppSpaces.Security.None.SHA1Portable;
#else
using System.Security.Cryptography;
#endif


namespace XcoAppSpaces.Transport.Sockets.Auth
{
	static class ByteExtensions
	{
		public static byte[] CreateHash(byte[] bytes, string salt)
		{
			if (bytes == null)
				return null;
			var hasher = new SHA1Managed(); //instance cannot be static because hasher not threadsafe

			byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
			byte[] hashed = hasher.ComputeHash(
				bytes.Concat(saltBytes).ToArray());

            hasher.Clear();
			return hashed;
		}

		public static bool ArrayEquals(byte[] b1, byte[] b2)
		{
			if (b1.Length != b2.Length)
				return false;
			for (int i = 0; i < b1.Length; i++)
			{
				if (b1[i] != b2[i])
					return false;
			}
			return true;
		}
	}
}